
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 10/25/2012 13:23:26
-- Generated from EDMX file: E:\GrooveWorkspace\Programming Worksapce\Free2Ask\Free2AskModel\Free2AskModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [Free2AskDB];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_Answer_inherits_Post]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Posts_Answer] DROP CONSTRAINT [FK_Answer_inherits_Post];
GO
IF OBJECT_ID(N'[dbo].[FK_Answers_Questions]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Posts_Answer] DROP CONSTRAINT [FK_Answers_Questions];
GO
IF OBJECT_ID(N'[dbo].[FK_PostFeedbacks_Feedback]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PostFeedbacks] DROP CONSTRAINT [FK_PostFeedbacks_Feedback];
GO
IF OBJECT_ID(N'[dbo].[FK_PostFeedbacks_Post]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PostFeedbacks] DROP CONSTRAINT [FK_PostFeedbacks_Post];
GO
IF OBJECT_ID(N'[dbo].[FK_PostTags_Post]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PostTags] DROP CONSTRAINT [FK_PostTags_Post];
GO
IF OBJECT_ID(N'[dbo].[FK_PostTags_Tag]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PostTags] DROP CONSTRAINT [FK_PostTags_Tag];
GO
IF OBJECT_ID(N'[dbo].[FK_Question_inherits_Post]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Posts_Question] DROP CONSTRAINT [FK_Question_inherits_Post];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Feedbacks]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Feedbacks];
GO
IF OBJECT_ID(N'[dbo].[PostFeedbacks]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PostFeedbacks];
GO
IF OBJECT_ID(N'[dbo].[Posts]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Posts];
GO
IF OBJECT_ID(N'[dbo].[Posts_Answer]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Posts_Answer];
GO
IF OBJECT_ID(N'[dbo].[Posts_Question]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Posts_Question];
GO
IF OBJECT_ID(N'[dbo].[PostTags]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PostTags];
GO
IF OBJECT_ID(N'[dbo].[Tags]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Tags];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Feedbacks'
CREATE TABLE [dbo].[Feedbacks] (
    [FeedbackContent] nvarchar(max)  NULL,
    [DatetimePosted] datetime  NULL,
    [ID] int IDENTITY(1,1) NOT NULL,
    [PostID] nvarchar(max)  NOT NULL,
    [Posts_ID] int  NOT NULL
);
GO

-- Creating table 'Posts'
CREATE TABLE [dbo].[Posts] (
    [ID] int IDENTITY(1,1) NOT NULL
);
GO

-- Creating table 'Tags'
CREATE TABLE [dbo].[Tags] (
    [Name] nvarchar(50)  NULL,
    [ID] int IDENTITY(1,1) NOT NULL
);
GO

-- Creating table 'Posts_Question'
CREATE TABLE [dbo].[Posts_Question] (
    [PostID] int  NOT NULL,
    [Title] nvarchar(500)  NULL,
    [PostContent] nvarchar(max)  NULL,
    [DatetimePosted] nvarchar(max)  NOT NULL,
    [ID] int  NOT NULL
);
GO

-- Creating table 'Posts_Answer'
CREATE TABLE [dbo].[Posts_Answer] (
    [PostID] int IDENTITY(1,1) NOT NULL,
    [QuestionID] int  NOT NULL,
    [Title] nvarchar(500)  NULL,
    [PostContent] nvarchar(max)  NULL,
    [DatetimePosted] nvarchar(max)  NOT NULL,
    [ID] int  NOT NULL
);
GO

-- Creating table 'PostTags'
CREATE TABLE [dbo].[PostTags] (
    [Posts_ID] int  NOT NULL,
    [Tags_ID] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [ID] in table 'Feedbacks'
ALTER TABLE [dbo].[Feedbacks]
ADD CONSTRAINT [PK_Feedbacks]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Posts'
ALTER TABLE [dbo].[Posts]
ADD CONSTRAINT [PK_Posts]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Tags'
ALTER TABLE [dbo].[Tags]
ADD CONSTRAINT [PK_Tags]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Posts_Question'
ALTER TABLE [dbo].[Posts_Question]
ADD CONSTRAINT [PK_Posts_Question]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Posts_Answer'
ALTER TABLE [dbo].[Posts_Answer]
ADD CONSTRAINT [PK_Posts_Answer]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [Posts_ID], [Tags_ID] in table 'PostTags'
ALTER TABLE [dbo].[PostTags]
ADD CONSTRAINT [PK_PostTags]
    PRIMARY KEY NONCLUSTERED ([Posts_ID], [Tags_ID] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [Posts_ID] in table 'Feedbacks'
ALTER TABLE [dbo].[Feedbacks]
ADD CONSTRAINT [FK_PostFeedbacks]
    FOREIGN KEY ([Posts_ID])
    REFERENCES [dbo].[Posts]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PostFeedbacks'
CREATE INDEX [IX_FK_PostFeedbacks]
ON [dbo].[Feedbacks]
    ([Posts_ID]);
GO

-- Creating foreign key on [Posts_ID] in table 'PostTags'
ALTER TABLE [dbo].[PostTags]
ADD CONSTRAINT [FK_PostTags_Post]
    FOREIGN KEY ([Posts_ID])
    REFERENCES [dbo].[Posts]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Tags_ID] in table 'PostTags'
ALTER TABLE [dbo].[PostTags]
ADD CONSTRAINT [FK_PostTags_Tag]
    FOREIGN KEY ([Tags_ID])
    REFERENCES [dbo].[Tags]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PostTags_Tag'
CREATE INDEX [IX_FK_PostTags_Tag]
ON [dbo].[PostTags]
    ([Tags_ID]);
GO

-- Creating foreign key on [QuestionID] in table 'Posts_Answer'
ALTER TABLE [dbo].[Posts_Answer]
ADD CONSTRAINT [FK_Answers_Questions]
    FOREIGN KEY ([QuestionID])
    REFERENCES [dbo].[Posts_Question]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Answers_Questions'
CREATE INDEX [IX_FK_Answers_Questions]
ON [dbo].[Posts_Answer]
    ([QuestionID]);
GO

-- Creating foreign key on [ID] in table 'Posts_Question'
ALTER TABLE [dbo].[Posts_Question]
ADD CONSTRAINT [FK_Question_inherits_Post]
    FOREIGN KEY ([ID])
    REFERENCES [dbo].[Posts]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [ID] in table 'Posts_Answer'
ALTER TABLE [dbo].[Posts_Answer]
ADD CONSTRAINT [FK_Answer_inherits_Post]
    FOREIGN KEY ([ID])
    REFERENCES [dbo].[Posts]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------